package com.atguigu.tingshu.search.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.search.AlbumInfoIndex;
import com.atguigu.tingshu.order.client.OrderInfoFeignClient;
import com.atguigu.tingshu.search.repository.AlbumInfoIndexRepository;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.MqOpsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class MqOpsServiceImpl implements MqOpsService {

    @Autowired
    private ItemService itemService;

    @Autowired
    private OrderInfoFeignClient orderInfoFeignClient;
    @Qualifier("com.atguigu.tingshu.album.client.AlbumInfoFeignClient")
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private AlbumInfoIndexRepository albumInfoIndexRepository;

    @Override
    public void albumUpper(String albumId) {
        itemService.albumOnSale(Long.parseLong(albumId));
        log.info("上架专辑到es成功");

    }

    @Override
    public void albumLower(String albumId) {
        itemService.albumOffSale(Long.parseLong(albumId));
        log.info("从es下架专辑成功");
    }

    @Override
    public void updateAlbumStatNum(JSONObject jsonObject) {
        String orderNo = (String) jsonObject.get("orderNo");
        Integer userId = (Integer) jsonObject.get("userId");
        // 1.根据订单编号
        Result<OrderInfo> orderInfoByOrderNo = orderInfoFeignClient.getOrderInfoByOrderNo(orderNo, Long.valueOf(userId));
        OrderInfo orderInfo = orderInfoByOrderNo.getData();
        Assert.notNull(orderInfo,"远程查询订单微服务获取订单信息失败");

        //2.获取付款项类型
        Long albumId = null;
        String itemType = orderInfo.getItemType();
        if ("1001".equals(itemType)){
            //专辑
            albumId = orderInfo.getOrderDetailList().get(0).getItemId();

        } else if ("1002".equals(itemType)) {
            //声音
            Long trackId = orderInfo.getOrderDetailList().get(0).getItemId();
            Result<TrackInfo> trackInfoResult = albumInfoFeignClient.getTrackInfoByTrackId(trackId);
            TrackInfo trackInfoResultData = trackInfoResult.getData();
            Assert.notNull(trackInfoResultData,"远程查询专辑微服务获取声音信息失败");
            albumId = trackInfoResultData.getAlbumId();
        }else {
            //vip不用管
            return;
        }

        //修改es
        Optional<AlbumInfoIndex> albumInfoIndexOptional = albumInfoIndexRepository.findById(albumId);
        albumInfoIndexOptional.ifPresent(albumInfoIndex -> {
            Integer buyStatNum = albumInfoIndex.getBuyStatNum();
            albumInfoIndex.setBuyStatNum(buyStatNum + orderInfo.getOrderDetailList().size());
            albumInfoIndexRepository.save(albumInfoIndex);
        });
    }
}
